﻿using System;
using gbL.Emit;

namespace gbL.NetBoard.Helpers
{
    public class SenderAdapter
    {
        public SenderAdapter(IBlackboard blackboard)
        {
            _blackboard = blackboard;
        }

        private readonly IBlackboard _blackboard;

        public IBlackboardEntry SendEntry(object message)
        {
            return _blackboard.Add(message);
        }

        public IBlackboardEntry SendEntry<T>(Action<T> initialise) where T : class
        {
            Generator generator = new Generator();
            T message = generator.Generate<T>();
            initialise(message);
            return _blackboard.Add(message);
        }

        public IBlackboardEntry SendEntry<T>() where T : class
        {
            return SendEntry<T>(x => { });
        }

        public T Send<T>(T message)
        {
            _blackboard.Add(message);
            return message;
        }

        public T Send<T>(Action<T> initialise) where T : class
        {
            return (T)SendEntry<T>(initialise).Message;
        }

        public T Send<T>() where T : class
        {
            return (T)SendEntry<T>().Message;
        }
    }
}
